package com.metagurukul.metaspace.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.metagurukul.metaspace.dao.DepartmentDao;
import com.metagurukul.metaspace.dao.ProjectDao;
import com.metagurukul.metaspace.dao.RoleDao;
import com.metagurukul.metaspace.dao.UserLoginDetailDao;
import com.metagurukul.metaspace.dao.UserPersonalDetailDao;
import com.metagurukul.metaspace.dao.UserProjectDao;
import com.metagurukul.metaspace.dao.UserWorkDetailDao;
import com.metagurukul.metaspace.models.beans.UserLoginDetail;
import com.metagurukul.metaspace.models.beans.UserPersonalDetail;
import com.metagurukul.metaspace.models.beans.UserProject;
import com.metagurukul.metaspace.models.beans.UserWorkDetail;

public final class UserService {

	private UserService() {
	}

	public static boolean checkValidUserLogin(UserLoginDetail userLoginDetail)
			throws SQLException {
		if(null == userLoginDetail){
			return false;
		}
		UserLoginDetailDao dao = new UserLoginDetailDao();
		return dao.loginValidate(userLoginDetail);
	}

	public static int getUserId(String emailId) throws SQLException {
		if(null == emailId){
			return -1;
		}
		UserLoginDetailDao dao = new UserLoginDetailDao();
		return dao.getUserId(emailId);
	}

	public static UserLoginDetail getUserLoginDetail(int userId)
			throws SQLException {
		if(-1 == userId){
			return null;
		}
		UserLoginDetailDao dao = new UserLoginDetailDao();
		return dao.getRecord(userId);
	}

	public static UserPersonalDetail getUserPersonalDetail(int userId)
			throws SQLException {
		if(-1 == userId){
			return null;
		}
		UserPersonalDetailDao dao = new UserPersonalDetailDao();
		return dao.getRecord(userId);
	}

	public static UserWorkDetail getUserWorkDetail(int userId)
			throws SQLException {
		if(-1 == userId){
			return null;
		}
		UserWorkDetailDao dao = new UserWorkDetailDao();
		UserWorkDetail userWorkDetail = new UserWorkDetail();
		userWorkDetail = dao.getRecord(userId);
		if (userWorkDetail == null) {
			return null;
		}
		int roleId = userWorkDetail.getRoleId();
		int deptId = userWorkDetail.getDeptId();
		RoleDao roleDao = new RoleDao();
		DepartmentDao departmentDao = new DepartmentDao();
		userWorkDetail.setRoleName(roleDao.getName(roleId));
		userWorkDetail.setDepartmentName(departmentDao.getName(deptId));
		UserProjectDao userProjectDao = new UserProjectDao();
		ArrayList<UserProject> userProjectList = (ArrayList<UserProject>) userProjectDao
				.getRecordList(userId);
		if (userProjectList != null) {
			for (int i = 0; i < userProjectList.size(); i++) {
				UserProject userProject = userProjectList.get(i);
				int projectId = userProject.getProjectId();
				ProjectDao projectDao = new ProjectDao();
				userProjectList.get(i).setProjectName(
						projectDao.getName(projectId));
			}
			userWorkDetail.setUserProjectList(userProjectList);
		}
		return userWorkDetail;
	}

	public static int insertPersonalDetail(UserPersonalDetail userPersonalDetail)
			throws SQLException {
		if(null == userPersonalDetail){
			return -1;
		}
		UserPersonalDetailDao dao = new UserPersonalDetailDao();
		return dao.addRecord(userPersonalDetail);
	}

	public static int insertUserLoginDetail(UserLoginDetail userLoginDetail)
			throws SQLException {
		if(null == userLoginDetail){
			return -1;
		}
		UserLoginDetailDao dao = new UserLoginDetailDao();
		if (dao.checkIsPresent(userLoginDetail.getEmailId())) {
			return -1;
		}
		return dao.insertRecord(userLoginDetail);
	}

	public static boolean insertWorkDetail(UserWorkDetail userWorkDetail)
			throws SQLException {
		if(null == userWorkDetail){
			return false;
		}
		UserWorkDetailDao dao = new UserWorkDetailDao();
		int flag = dao.addRecord(userWorkDetail);
		if (flag > 0) {
			UserProjectDao userProjectDao = new UserProjectDao();
			ArrayList<UserProject> userProjectList = (ArrayList<UserProject>) userWorkDetail
					.getUserProjectList();
			for (int i = 0; i < userProjectList.size(); i++) {
				int userId = userProjectList.get(i).getUserId();
				if(!userProjectDao.checkIsPresent(userId)){
					userProjectDao.insertRecord(userProjectList.get(i));
				} else{
					userProjectDao.updateRecord(userProjectList.get(i));
				}
			}
			return true;
		}
		return false;
	}

	public static List<UserPersonalDetail> searchUserList(String searchText)
			throws SQLException {
		UserPersonalDetailDao userPersonalDetailDao = new UserPersonalDetailDao();
		return userPersonalDetailDao.getPersonalRecordList(searchText);

	}

	public static int updateProfilePic(String path, int userId)
			throws SQLException {
		if(-1 == userId){
			return -1;
		}
		UserPersonalDetailDao dao = new UserPersonalDetailDao();
		return dao.updateProfilePic(path, userId);
	}

	public static int updateUserLoginDetail(UserLoginDetail userLoginDetail)
			throws SQLException {
		if(null == userLoginDetail){
			return -1;
		}
		UserLoginDetailDao dao = new UserLoginDetailDao();
		return dao.updateRecord(userLoginDetail);

	}

	public static int updateUserLoginDetail(UserLoginDetail userLoginDetail,
			String oldPassword) throws SQLException {
		if(null == userLoginDetail){
			return -1;
		}
		UserLoginDetailDao dao = new UserLoginDetailDao();
		if (!dao.checkIsPresent(userLoginDetail.getUserId())) {
			return -1;
		}
		return dao.updateRecord(userLoginDetail, oldPassword);
	}
	public static List<UserPersonalDetail> getUserPersonalDetailList() throws SQLException{
		UserPersonalDetailDao userPersonalDetailDao = new UserPersonalDetailDao();
		return userPersonalDetailDao.getRecordList();
	}
}
